If loop with multiple conditions still executing when one condition is not met